package com.seocoo.worker.user.account;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.UtilStatus;
import com.seocoo.worker.base.widget.AppToolBer;
import com.seocoo.worker.user.account.entity.EBankCar;
import com.seocoo.worker.user.account.model.CardModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;

/***
 * 我的银行卡
 */
public class MyBankCarActivity extends MVVMActivity {

    public static void start(Activity context, int requestCode) {
        Intent intent = new Intent(context, MyBankCarActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    public static void start(Fragment context, int requestCode) {
        Intent intent = new Intent(context.getContext(), MyBankCarActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    private AppToolBer appToolBer;
    private TextView tvName;
    private TextView tvNumber;
    private TextView tvOk;

    EBankCar mBankCar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_bankcar_activity);
        UtilStatus.initStatus(this, Color.WHITE);

        getViewModel(CardModel.class).queryCard();
        initView();
    }

    @LiveDataMatch
    public void onBankCar(EBankCar eBankCar) {
        this.mBankCar = eBankCar;
        if (eBankCar != null) {
            tvName.setText("**** **** **** " + eBankCar.bankName.substring(eBankCar.bankName.length() - 4));
            tvNumber.setText(eBankCar.cardNo);
        }
    }

    @LiveDataMatch
    public void onUnBundCardSuccess() {
        //解绑成功
        setResult(Activity.RESULT_OK);
        finish();
    }

    private void initView() {
        appToolBer = (AppToolBer) findViewById(R.id.appToolBer);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        tvOk.setOnClickListener(v -> {
            if (mBankCar != null) {
                DialogBuilder dialogBuilder = new DialogBuilder();
                dialogBuilder.setMessage("您确定解除绑定银行卡吗？");
                dialogBuilder.setOkListener((dialog, which) -> {
                    if (mBankCar != null) {
                        getViewModel(CardModel.class).unbundCard();
                    }
                });
                showDialog(dialogBuilder);
            } else {
                showDialogToast("当前未绑定银行卡！");
            }

        });
    }
}
